package com.pfzy.ui.utils;

import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public static String getDayText(Long time, int delay) {
        Calendar calen = getCalendar(time, delay);
        return getDayText(calen);
    }

    public static String getDayText(Calendar calen) {
        StringBuffer str = new StringBuffer();
        int year = calen.get(Calendar.YEAR);
        str.append(year+"-");

        int month = calen.get(Calendar.MONTH)+1;
        if(month<10){
            str.append("0"+month);
        }
        else{
            str.append(""+month);
        }

        int day=calen.get(Calendar.DAY_OF_MONTH);
        if(day<10){
            str.append("-0"+day);
        }
        else{
            str.append("-"+day);
        }
        return str.toString();
    }

    public static Calendar getCalendar(Long time, int delay)  {
        Long newTime = getDateDelay(time, delay);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(newTime);
        return calendar;
    }

    private static Long getDateDelay(Long time,int delay)  {
        return time+86400000*delay;
    }

    /**
     * 获得当天零时零分零秒
     * @return
     */
    public static Date initDateByDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
    /**
     * 获得明天零时零分零秒
     * @return
     */
    public static long getTimeByDay(int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.DATE,day);
        return calendar.getTimeInMillis();
    }
    /**
     * 当天的结束时间
     * @return
     */
    public static Date endOfTodDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        Date date=calendar.getTime();
        return date;
    }

    /**
     * 几天后 开始结束时间
     * @param delay
     * @return
     */
    public static Calendar getCalendarDayStart(int delay)  {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.DATE,delay);
        return calendar;
    }
    public static Calendar getCalendarDayEnd(int delay)  {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.add(Calendar.DATE,delay);
        return calendar;
    }
}
